home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / LIBRARY / PCTV3N5 / NEWDEBUG.H < prev    next >
C/C++ Source or Header  |  1992-09-29  |  3KB  |  110 lines

  1. /////////////////////////////////////////////////
  2. //  DEBUG DYNAMIC MEMORY ALLOCATION 
  3. //      newdebug.h
  4. //
  5. //      Implements a debugging replacement for 
  6. //      the standard C++ memory allocation 
  7. //      operators.
  8. //
  9. //      Tested with:
  10. //          Microsoft C/C++ 7.00
  11. //          Borland C++ 3.x
  12. //
  13. //      Copyright 1992 Scott Robert Ladd
  14. //      All Rights Reserved
  15. /////////////////////////////////////////////////
  16.  
  17. #ifndef NEWDEBUG_H
  18. #define NEWDEBUG_H
  19.  
  20. #include "stddef.h"
  21.  
  22. //-----------------------------------------------
  23. // NewDbg
  24. //      This class encapsulates the tools used in 
  25. //      tracking and verifying dynamic memory 
  26. //      allocations. "new" and "delete" are 
  27. //      friends to provide access to the 
  28. //      protected members of NewDbg.
  29. //-----------------------------------------------
  30.  
  31. class NewDbg
  32.     {
  33.     friend void * operator new (size_t size);
  34.     friend void operator delete (void * ptr);
  35.  
  36.     public:
  37.         // error codes
  38.         enum Errors
  39.             {
  40.             EF_OKAY,
  41.             EF_ZEROSIZE,
  42.             EF_NOTALLOC,
  43.             EF_INVALID,
  44.             EF_UNDERFLOW,
  45.             EF_TOOMANY
  46.             };
  47.  
  48.         // returns number of active allocations
  49.         static long GetCount();
  50.  
  51.         // returns number of bytes allocated
  52.         static long GetBytes();
  53.  
  54.         // returns non-zero if ptr
  55.         // was allocated with new
  56.         static int IsDynamic(void * ptr);
  57.  
  58.         // returns the current error code
  59.         static Errors GetError();
  60.  
  61.         // sets the error code to EF_OKAY
  62.         static void ClearError();
  63.  
  64.         // sets the function called on error
  65.         static void InstallHandler(
  66.                      void (* func)(Errors flag));
  67.  
  68.         // called when an error occurs
  69.         static void ReportError(Errors err);
  70.  
  71.     protected:
  72.  
  73.         // header data for tracking allocations
  74.         struct AllocHeader
  75.             {
  76.             unsigned short Marker;
  77.             size_t         Bytes;
  78.             };
  79.  
  80.         // number of items allocated
  81.         static long Count;
  82.  
  83.         // number of bytes allocated
  84.         static long Bytes;
  85.  
  86.         // current error code
  87.         static Errors ErrorFlag;
  88.  
  89.         // allocation marker
  90.         static const unsigned short CorrectMark;
  91.  
  92.         // pointer to function called on error
  93.         static void (* ErrorFunc)(Errors flag);
  94.     };
  95.  
  96. // pointer to C++ error handler
  97. extern void (* _new_handler)();
  98.  
  99. // function to set _new_handler
  100. void (* set_new_handler(void (* handler)()))();
  101.  
  102. // synonym for set_new_handler
  103. #define _set_new_handler(ptr) set_new_handler(ptr)
  104.  
  105. // debugging implementations of new and delete
  106. void * operator new (size_t size);
  107. void operator delete (void * ptr);
  108.  
  109. #endif
  110.